package com.runen.wnhz.runen.ui.fragment.major;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.NetUtils;
import com.runen.wnhz.runen.common.utils.OkHttpUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.UserBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

public class UserNoticeFragment extends Fragment {

    private String token;

    private int currentPage = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(getUserVisibleHint() && getActivity() != null){
            getUserNotice(String.valueOf(currentPage));
        }
    }

    private void initData(){
        ACacheUtlis cacheUtlis = ACacheUtlis.getInstance();
        if(cacheUtlis != null){
            UserBean reqeustUser = cacheUtlis.getReqeustUser(getActivity());
            if(reqeustUser != null){
                token = reqeustUser.getToken();
            }
        }
    }

    private void getUserNotice(String pageNum){
        ToastUtil.showToast("暂无内容");
        if(token != null) {
            FormBody formBody = new FormBody.Builder().add("token", token).add("type","2").add("page",pageNum).build();
            Request request = new Request.Builder().url(NetUtils.USER_MESSAGE_CENTER).post(formBody).build();
            Call newCall = OkHttpUtils.getOkHttpClient().newCall(request);
            newCall.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showToast("用户通知数据请求失败");
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                }
            });
        }
    }

    private void handleUserNoticeData(Response response)  throws IOException {


    }


}
